package com.example.antitouchscreen;

import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.IBinder;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
//import androidx.appcompat.app.AlertDialog;
import androidx.core.app.NotificationManagerCompat;

import static com.example.antitouchscreen.MainActivity.NOTIFICATION_ID;

public class NotificationActionService extends Service {
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String action = intent.getAction();
        if ("ANTI_TOUCH".equals(action)) {
            // 启动FloatingService
            startService(new Intent(this, FloatingService.class));
            // 关闭通知栏
//            NotificationManagerCompat.from(this).cancel(NOTIFICATION_ID);
        } else if ("TIMER".equals(action)) {
            long totalMillis = intent.getLongExtra("total_seconds", 0L);

            // 计算定时时间，这里使用totalMillis来设置更准确的定时时间，替换原来简单的固定值15000毫秒
            long triggerAtMillis = System.currentTimeMillis() + totalMillis;
            // 设置定时关闭悬浮窗
            setTimerToCloseFloatingService(triggerAtMillis);
            // 启动FloatingService
            startService(new Intent(NotificationActionService.this, FloatingService.class));


            // 显示定时对话框
//            showTimerDialog();
        }else if("CLOSE".equals(action)){
            MyLog.d("进入了关闭");
//            getApplicationContext().stopService(new Intent(getApplicationContext(),FloatingService.class));
            stopService(new Intent(getApplicationContext(),FloatingService.class));

        }
        return START_NOT_STICKY;
    }

    private void showTimerDialog() {
        // 这里只是一个示例，您需要根据实际情况实现对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("设置定时");

        // 创建输入框
        final EditText inputHours = new EditText(this);
        inputHours.setInputType(InputType.TYPE_CLASS_NUMBER);
        inputHours.setHint("小时");
        final EditText inputMinutes = new EditText(this);
        inputMinutes.setInputType(InputType.TYPE_CLASS_NUMBER);
        inputMinutes.setHint("分钟");

        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(inputHours);
        layout.addView(inputMinutes);
        builder.setView(layout);

        // 设置确定按钮
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 获取输入的小时和分钟
                int hours = Integer.parseInt(inputHours.getText().toString());
                int minutes = Integer.parseInt(inputMinutes.getText().toString());
                // 计算定时时间
                long triggerAtMillis = System.currentTimeMillis() + (hours * 60 + minutes) * 60 * 1000;
                // 设置定时关闭悬浮窗
                setTimerToCloseFloatingService(triggerAtMillis);
                // 启动FloatingService
                startService(new Intent(NotificationActionService.this, FloatingService.class));
                // 关闭通知栏
                NotificationManagerCompat.from(NotificationActionService.this).cancel(NOTIFICATION_ID);
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        builder.show();
    }

    private void setTimerToCloseFloatingService(long triggerAtMillis) {
        Intent intent = new Intent(this, TimerReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, triggerAtMillis, pendingIntent);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
